package ${packageName}.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Date;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import com.google.common.collect.Lists;
import cn.com.gc.base.core.db.DynamicDataSource;
import Page;
import ${packageName}.cache.${name}Cache;
import ${packageName}.model.${name};

/**
 * @ClassName: ${name}Dao.java
 * @Description: ${description}的数据交互处理类
 * @author ${author}
 * @date   ${newdate}
 * @version V1.0.0
 * <p>
 * ----------------------------------------------------------------------------
 *  modifyer    modifyTime                 comment
 *
 * ----------------------------------------------------------------------------
 * </p>
*/
@Component
public class ${name}Dao {

	@Autowired
	private ${dataSource} ${dataSourceLower};
	
	private final String SQL = "SELECT  ${columnStr} ";
	
	
	
		/**
	 * 通过主键${nameLower}Id查找${name}
	 * @param ${nameLower}
	 */	
	public ${name} get${name}(${name} ${nameLower}){
		if(null == ${nameLower} || ${nameLower}.getId() < 0){ 
		   return  null;
		}
		long id = ${nameLower}.getId();
		String sql = SQL
		              + " FROM ${table}  WHERE id = ? ";
		              
		${nameLower} = ${dataSourceLower}.queryT(sql, ${nameLower}Mapper, id);
		if(null == ${nameLower}){
			return null;
		}
		return  ${nameLower};
	}
	
	/**
	 * 通过主键${nameLower}Id查找${name}
	 * @param ${nameLower}Id
	 */	
	public ${name} get${name}ById(long ${nameLower}Id){
		if(${nameLower}Id <= 0) 
		   return  null;
		String sql = SQL
		       + " FROM ${table}  WHERE id = ? ";
		              
		${name} ${nameLower} = ${dataSourceLower}.queryT(sql, ${nameLower}Mapper, ${nameLower}Id);
		if(null == ${nameLower}){
			return null;
		}
		return  ${nameLower};
	}
	
	
	/**
	 * 通过主键id集合批量查找${name} Collection<Long> 
	 * @param colle
	 */	
	public List<${name}>  get${name}ListByIdList(Collection<Long> colle){
		if(null == colle || colle.isEmpty()) {
		   return  null;
		}
		List<${name}> list = new ArrayList<>();
		//查找从缓存中没有查找到的${name}实现;
		String sql = SQL 
		          + " FROM ${table}  WHERE id IN ( ? ) ";
		
		if(colle.size() > ${dataSourceLower}.LISTS_PARTITON_SIZE){
			/*** 如果大于100条,则查用分页查询;返回结果值; */
			List<List<Long>> partition = Lists.partition(new ArrayList<>(colle),  ${dataSourceLower}.LISTS_PARTITON_SIZE);
			if(CollectionUtils.isNotEmpty(partition)){
				for(List<Long> colles : partition ){
					List<${name}>  dblist = ${dataSourceLower}.queryByInToList(sql, ${nameLower}Mapper, colles);
					if(null != dblist && !dblist.isEmpty()){
						list.addAll(dblist);
					}
				}
				return list;
			}
		}else{
			List<${name}>  dblist = ${dataSourceLower}.queryByInToList(sql, ${nameLower}Mapper, colle);
			if(null != dblist && !dblist.isEmpty()){
				return dblist;
			}
		}
		return  list;
	}
		
	/**
	 * 通过主键id集合批量查找${name}
	 * @param colle
	 */	
	public Map<Long,${name}>  get${name}MapByIdList(Collection<Long> colle){
		int pageSize = 100;
		if(CollectionUtils.isEmpty(colle)){
			return new HashMap<Long, ${name}>();
		}
		
		List<${name}> list = new ArrayList<>();
		String sql = SQL
				+ " FROM ${table}  WHERE id in ( ? )  ";
		/** 如果批量id 少于指定值时100条,直接查询**/
		if(colle.size() < pageSize && !colle.isEmpty()){
			list = ${dataSourceLower}.queryByInToList(sql, ${nameLower}Mapper, colle);
		}else{
			/*** 如果大于100条,则查用分页查询;返回结果值; */
			List<List<Long>> partition = Lists.partition(new ArrayList<>(colle), pageSize);
			if(CollectionUtils.isNotEmpty(partition)){
				for(List<Long> ids : partition ){
					List<${name}> dblist = ${dataSourceLower}.queryByInToList(sql,${nameLower}Mapper, ids);
					if(null != dblist && !dblist.isEmpty()){
						list.addAll(dblist);
					}
				}
			}
			
		}
		//从db中批量查找作品信息;
		if(CollectionUtils.isNotEmpty(list)){
			for (${name} ${nameLower} : list) {
				map.put(${nameLower}.getId(), ${nameLower});
			}
		} 
		return map;
	}
	/**
	 * 通过分页Pager查找${name}
	 * @param page
	 */	
	 @Deprecated
	public List<${name}>  get${name}ListByPager(Pager page){
		if(null == page ) {
		   return  null;
		}
		//需要自己实现相关的分页和缓存业务
		String sql = SQL
		        + " FROM ${table}  WHERE 1 =1 limit ?,?  ";
	
		List<${name}> list = ${dataSourceLower}.query(sql, ${nameLower}Mapper,page.getStart(),page.getPageSize());
		if(null != list && !list.isEmpty()){
		    return  list;
		}
		 return  null;
		
	}
	
	
	
	/**
	 * 查询使用映射对象Mapper
	 * @param ${nameLower}Mapper
	 */
	private RowMapper<${name}> ${nameLower}Mapper = new RowMapper<${name}>() {
		@Override
		public ${name} mapRow(ResultSet rs, int rowNum) throws SQLException {
			${name} vo = new ${name}();${vo}			
			return vo;
		}
	};

}
